package com.map.view;
import java.util.ArrayList;
import com.xl.game.tool.XL;
import android.graphics.Rect;
/*
地图对象层
包含
 <objectgroup name="bounds">
 <object id="12" x="0" y="0" width="3" height="640"/>
 <object id="14" x="2" y="638" width="960" height="3"/>
 <object id="16" x="960" y="0" width="3" height="638"/>
 </objectgroup>

*/
public class MapObjectGroup implements Map
{
	String name="";
	ArrayList<MapObject> list_obj;
	boolean isVisible;
	
	public MapObjectGroup(){
		this.list_obj = new ArrayList<MapObject>();
		this.name ="";
		this.isVisible = true;
	}
	
	@Override
	public void setVisible(boolean bisible)
	{
		// TODO: Implement this method
		this.isVisible = bisible;
	}

	@Override
	public boolean getVisible()
	{
		// TODO: Implement this method
		return isVisible;
	}


	@Override
	public String getName()
	{
		// TODO: Implement this method
		return this.name;
	}

	
	
	
	class MapObject
	{
		public MapObject(int id,int x,int y,int width,int height){
			this.id=id;
			this.x = x;
			this.y= y;
			this.width = width;
			this.height = height;
		}
		int id;
		int x;
		int y;
		int width;
		int height;
		
	}
	
	//设置图层名称
	public void setName(String name){
		this.name = name;
	}
	
	//添加一个obj
	public void addObject(int id,int x,int y,int width,int height){
		MapObject obj = new MapObject(id,x,y,width,height);
		list_obj.add(obj);
	}
	//修改第i个obj
	public void setObj(int i,MapObject obj){
		list_obj.remove(i);
		list_obj.add(i,obj);
	}
	
	//获取第n个obj
	public MapObject get(int i){
		return list_obj.get(i);
	}
	
	//删除一个obj
	public void remove(int i){
		list_obj.remove(i);
	}
	//清空obj
	public void clear(){
		list_obj.clear();
	}
	
	//判断是否点中
	public boolean contains(int x,int y){
		for(int i=0;i<list_obj.size();i++){
			MapObject obj =  list_obj.get(i);
			Rect rect = new Rect(obj.x, obj.y, obj.x + obj.width, obj.y + obj.height);
			if(rect.contains(x,y))
				return true;
		}
		return false;
	}

	//
	public int size(){
		return list_obj.size();
	}
	
	
	//输出本图层的tmx格式
	//toString
	@Override
	public String toString()
	{
		// TODO: Implement this method
		StringBuilder buf = new StringBuilder();
		buf.append(XL.sprintf("  <objectgroup name=\"%s\">\r\n",this.name));
		for(int i=0;i<list_obj.size();i++){
			MapObject obj = list_obj.get(i);
			buf.append(XL.sprintf("   <object id=\"%d\" x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\"/>\r\n", obj.id,obj.x,obj.y,obj.width,obj.height));
		}
		
		buf.append("  </objectgroup>\r\n");
		return buf.toString();
	}
}
